//
//  ViewController.m
//  10103-九宫格
//
//  Created by student on 16/2/27.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "goodsModel.h"
#import "SubView.h"
#import "SubViewController.h"

@interface ViewController ()

@property (nonatomic,strong) NSArray *goodsData;

@end

@implementation ViewController

-(NSArray *)goodsData{
    if (_goodsData == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"goods.plist" ofType:nil];
        NSArray *tempArr = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *mutArr = [NSMutableArray array];
        for (NSDictionary *dict in tempArr) {
            goodsModel *goods = [goodsModel goodsModelWithDict:dict];
            [mutArr addObject:goods];
        }
        _goodsData = mutArr;
    }
    return _goodsData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self setUpUI];
}

-(void)setUpUI{
    
    for (int i = 0; i < self.goodsData.count; i++) {
        //创建View
        
        //设定View的位置
        int line = 2;
        //根据行确定当前列和行
        int col = i % line;
        int row = i / line;
        //根据每行放几个，计算当前商品在第几行第几列
        int viewH = 180;
        int viewW = 180;
        SubView *sbv = [[SubView alloc] init];

        //SubViewController *sbv = [[SubViewController alloc] init];
//
//
        
     [sbv setFrame:CGRectMake(18*(col+1)+viewW*col, 44+viewH*row+18*row, viewW, viewH)];
        goodsModel *goods = self.goodsData[i];
        
        [sbv.imageView1 setImage:[UIImage imageNamed:goods.picture]];
        //[sbv.imageView1 setPreservesSuperviewLayoutMargins:YES];
        [sbv.imageView2 setImage:[UIImage imageNamed:@"tfw_gwc"]];
        //[sbv.imageView2 setPreservesSuperviewLayoutMargins:YES];
        NSString *str1 = [NSString stringWithFormat:@"总共需要%@份",goods.totalCount];
        [sbv.label1 setFont:[UIFont systemFontOfSize:12]];
        [sbv.label1 setText:str1];
        NSString *str2 = [NSString stringWithFormat:@"已购%@份",goods.alreadyCount];
        [sbv.label2 setFont:[UIFont systemFontOfSize:12]];
        [sbv.label2 setText:str2];
        //[sbv.label2 setPreservesSuperviewLayoutMargins:YES];
        [sbv setBackgroundColor:[UIColor whiteColor]];
        
        [self.view addSubview:sbv];
        
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
